package org.jeecg.modules.front.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.common.value.qual.DoubleVal;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.jeecg.common.util.encryption.EncryptedString;
import org.jeecg.config.JeecgBaseConfig;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.front.entity.vo.*;
import org.jeecg.modules.fund.entity.UserhomeFund;
import org.jeecg.modules.fund.service.IUserhomeFundService;
import org.jeecg.modules.gongqiang.entity.Userhome;
import org.jeecg.modules.gongqiang.service.IUserhomeService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysRoleIndex;
import org.jeecg.modules.system.entity.SysTenant;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.model.SysLoginModel;
import org.jeecg.modules.system.service.*;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.jeecg.modules.system.util.RandImageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author scott
 * @since 2018-12-17
 */
@RestController
@RequestMapping("/front/user")
@Api(tags = "前台用户基础模块")
@Slf4j
public class LoginUserhomeController {
    @Autowired
    private IUserhomeService userhomeService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SysBaseApiImpl sysBaseApi;
    @Autowired
    private IUserhomeFundService userhomeFundService;
    @ApiOperation("登录接口")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<JSONObject> login(@RequestBody LoginUserhome loginUserhome) {
        Result<JSONObject> result = new Result<JSONObject>();
        String mail = loginUserhome.getMail();
        String password = loginUserhome.getPassword();
        String code = loginUserhome.getCode();
        Integer loginType = loginUserhome.getLoginType();
        if (oConvertUtils.isEmpty(mail)) {
            result.setMessage("邮箱不允许为空！");
            result.setSuccess(false);
            return result;
        }
        LambdaQueryWrapper<Userhome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userhome::getUsername, mail);
        Userhome userhome = userhomeService.getOne(queryWrapper);
        if (userhome == null) {
            result.error500("您还未注册，请先注册账号");
            return result;
        }
        if (loginType.equals(0)) {
            if (code == null) {
                result.error500("验证码无效");
                return result;
            }
            Object checkCode = redisUtil.get("FULOGIN" + mail);
            if (checkCode == null || !checkCode.toString().equals(code)) {
                result.error500("验证码错误");
                result.setCode(HttpStatus.PRECONDITION_FAILED.value());
                return result;
            }
            redisUtil.del("FULOGIN" + mail);
        }
        if (userhome == null) {
            result.error500("您还未注册，请先注册账号");
        } else {
            if(userhome.getStatus()==0)
            {
                result.error500("该用户禁止登录");
                return result;
            }
            result = userhomeService.checkUserIsEffective(userhome);
            if (!result.isSuccess()) {
                return result;
            }
            if (loginType.equals(0)) {

            } else {
                //2. 校验用户名或密码是否正确
                String userpassword = PasswordUtil.encrypt(String.valueOf(userhome.getId()), password, userhome.getSalt());
                String syspassword = userhome.getPassword();
                if (!syspassword.equals(userpassword)) {
                    result.error500("用户名或密码错误");
                    return result;
                }
            }
        }
        //用户登录信息
        userInfo(userhome, result);
        userhome.setPassword(null);
        userhome.setSalt(null);
        return result;
    }

    /**
     * 用户信息
     *
     * @param
     * @param result
     * @return
     */
    private Result<JSONObject> userInfo(Userhome userhome, Result<JSONObject> result) {
        String phone = userhome.getPhone();
        String syspassword = userhome.getPassword();
        JSONObject obj = new JSONObject(new LinkedHashMap<>());
        // 生成token
        String token = JwtUtil.frontSign(phone, syspassword, userhome.getId());
        // 设置token缓存有效时间
        redisUtil.set("AT" + userhome.getId() + userhome.getPhone(), token);
        redisUtil.expire("AT" + userhome.getId() + userhome.getPhone(), JwtUtil.EXPIRE_TIME * 2 / 1000);
        userhomeService.setAccessToken(userhome.getId(), token);
        obj.put("token", token);
        obj.put("userInfo", userhome);
        result.setResult(obj);
        result.success("登录成功");
        return result;
    }


//    @ApiOperation("注册接口-记得加点击次数限制")
//    @RequestMapping(value = "/register", method = RequestMethod.POST)
//    public Result<JSONObject> register(@RequestBody RegisterUserhome registerUserhome) {
//        Result<JSONObject> result = new Result<JSONObject>();
//        String username = registerUserhome.getUsername();
//        String phone = registerUserhome.getPhone();
//        String password = registerUserhome.getPassword();
//        String code = registerUserhome.getCode();
//        if (oConvertUtils.isEmpty(phone)) {
//            result.setMessage("手机号不允许为空！");
//            result.setSuccess(false);
//            return result;
//        }
//        if (oConvertUtils.isEmpty(password)) {
//            result.setMessage("密码不允许为空！");
//            result.setSuccess(false);
//            return result;
//        }
//        if (code == null) {
//            result.error500("验证码无效");
//            return result;
//        }
//        Object checkCode = redisUtil.get("FULOGIN" + phone);
//        if (checkCode == null || !checkCode.toString().equals(code)) {
//            result.error500("验证码错误");
//            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
//            return result;
//        }
//        redisUtil.del("FULOGIN" + phone);
//        LambdaQueryWrapper<Userhome> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Userhome::getUsername, phone);
//        Userhome userhome = userhomeService.getOne(queryWrapper);
//        if (userhome != null) {
//            result.error500("手机号已被注册");
//            return result;
//        }
//        userhome = new Userhome();
//        userhome.setUsername(username);
//        userhome.setPhone(phone);
//        userhome.setStatus(1);
//        userhome.setCreateTime(new Date());
//        userhome.setUpdateTime(new Date());
//        userhome.setPassword(password);
//        Random random = new Random();
//        userhome.setSalt(String.valueOf(random.nextInt(90000000)+10000000));
//        userhomeService.save(userhome);
//        String userpassword = PasswordUtil.encrypt(String.valueOf(userhome.getId()), password, userhome.getSalt());
//        userhome.setPassword(userpassword);
//        userhomeService.updateById(userhome);
//        //用户登录信息
//        userInfo(userhome, result);
//        userhome.setPassword(null);
//        userhome.setSalt(null);
//        return result;
//    }

    @ApiOperation("个人信息")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public Result<Userhome> info(HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        Userhome userhome = userhomeService.getById(userId);
        //算总收益
        QueryWrapper<UserhomeFund>  queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userhome_id",userId);
        queryWrapper.select("SUM(profit_money) as sumProfitMoney"); // 指定要求和的字段
        List<Object> results = userhomeFundService.listObjs(queryWrapper); // 执行查询
        if(!results.isEmpty()) {
            userhome.setProfitMoney((Double) (results.get(0)));
        }
        else
        {
            userhome.setProfitMoney(0.00);
        }
        userhome.setPassword(null);
        userhome.setSalt(null);
        return Result.ok(userhome);
    }


    @ApiOperation("修改密码")
    @RequestMapping(value = "/password/up", method = RequestMethod.POST)
    public Result<JSONObject> upPassword(@RequestBody RegisterUserhome registerUserhome, HttpServletRequest req) {
        Result<JSONObject> result = new Result<JSONObject>();
        String mail = registerUserhome.getMail();
        String phone = registerUserhome.getPhone();
        String password = registerUserhome.getPassword();
        //String oldPassword = registerUserhome.getOldPassword();
        String code = registerUserhome.getCode();
        if (oConvertUtils.isEmpty(phone)) {
            result.setMessage("手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }
        if (oConvertUtils.isEmpty(mail)) {
            result.setMessage("邮箱不允许为空！");
            result.setSuccess(false);
            return result;
        }
        if (code == null) {
            result.error500("验证码无效");
            return result;
        }
        if (oConvertUtils.isEmpty(password)) {
            result.setMessage("新密码不允许为空！");
            result.setSuccess(false);
            return result;
        }
//        if (oConvertUtils.isEmpty(oldPassword)) {
//            result.setMessage("旧密码不允许为空！");
//            result.setSuccess(false);
//            return result;
//        }
        Object checkCode = redisUtil.get("FULOGIN" + mail);
        if (checkCode == null || !checkCode.toString().equals(code)) {
            result.error500("验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        redisUtil.del("FULOGIN" + mail);
        LambdaQueryWrapper<Userhome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userhome::getUsername, mail);
        Userhome userhome = userhomeService.getOne(queryWrapper);
        result = userhomeService.checkUserIsEffective(userhome);
        if (!result.isSuccess()) {
            return result;
        }
//        //2. 校验用户名或密码是否正确
//        String userpassword = PasswordUtil.encrypt(String.valueOf(userhome.getId()), oldPassword, userhome.getSalt());
//        String syspassword = userhome.getPassword();
//        if (!syspassword.equals(userpassword)) {
//            result.error500("旧密码错误");
//            return result;
//        }
        Random random = new Random();
        userhome.setSalt(String.valueOf(random.nextInt(90000000) + 10000000));
        String userpassword = PasswordUtil.encrypt(String.valueOf(userhome.getId()), password, userhome.getSalt());
        userhome.setPassword(userpassword);
        userhome.setUpdateTime(new Date());
        userhome.setPhone(phone);
        userhomeService.updateById(userhome);
        result.success("密码修改成功");
        redisUtil.del("AT" + userhome.getId() + userhome.getPhone());
        userhomeService.setAccessToken(userhome.getId(), "token");
        return result;
    }


    @ApiOperation("忘记密码")
    @RequestMapping(value = "/password/forget", method = RequestMethod.POST)
    public Result<JSONObject> forgetPassword(@RequestBody RegisterUserhome registerUserhome, HttpServletRequest req) {
        Result<JSONObject> result = new Result<JSONObject>();
        String mail = registerUserhome.getMail();
        String password = registerUserhome.getPassword();
        String code = registerUserhome.getCode();
        if (code == null) {
            result.error500("验证码无效");
            return result;
        }
        if (oConvertUtils.isEmpty(password)) {
            result.setMessage("新密码不允许为空！");
            result.setSuccess(false);
            return result;
        }
        Object checkCode = redisUtil.get("FULOGIN" + mail);
        if (checkCode == null || !checkCode.toString().equals(code)) {
            result.error500("验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        redisUtil.del("FULOGIN" + mail);
        LambdaQueryWrapper<Userhome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userhome::getUsername, mail);
        Userhome userhome = userhomeService.getOne(queryWrapper);
        result = userhomeService.checkUserIsEffective(userhome);
        if (!result.isSuccess()) {
            return result;
        }
        Random random = new Random();
        userhome.setSalt(String.valueOf(random.nextInt(90000000) + 10000000));
        String userpassword = PasswordUtil.encrypt(String.valueOf(userhome.getId()), password, userhome.getSalt());
        userhome.setPassword(userpassword);
        userhome.setUpdateTime(new Date());
        userhomeService.updateById(userhome);
        result.success("密码修改成功");
        redisUtil.del("AT" + userhome.getId() + userhome.getPhone());
        userhomeService.setAccessToken(userhome.getId(), "token");
        return result;
    }

    @ApiOperation("更换手机号-确定按钮记得做连点限制，不要出现一连两次请求")
    @RequestMapping(value = "/phone", method = RequestMethod.POST)
    public Result<JSONObject> phone(@RequestBody UpdateUserhomePhone updateUserhomePhone, HttpServletRequest req) {
        Result<JSONObject> result = new Result<JSONObject>();
        String phone = updateUserhomePhone.getPhone();
        String code = updateUserhomePhone.getCode();
        String newPhone = updateUserhomePhone.getNewPhone();
        String newCode = updateUserhomePhone.getNewCode();
        LambdaQueryWrapper<Userhome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userhome::getUsername, newPhone);
        Userhome userhome = userhomeService.getOne(queryWrapper);
        if (userhome != null) {
            result.error500("手机号已被使用");
            return result;
        }
        if (oConvertUtils.isEmpty(phone)) {
            result.setMessage("旧手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }
        if (code == null) {
            result.error500("旧手机验证码无效");
            return result;
        }
        if (oConvertUtils.isEmpty(newPhone)) {
            result.setMessage("新手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }
        if (newCode == null) {
            result.error500("新手机验证码无效");
            return result;
        }
        Object checkCode = redisUtil.get("FULOGIN" + phone);
        if (checkCode == null || !checkCode.toString().equals(code)) {
            result.error500("旧手机验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        checkCode = redisUtil.get("FULOGIN" + newPhone);
        if (checkCode == null || !checkCode.toString().equals(newCode)) {
            result.error500("新手机验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        redisUtil.del("FULOGIN" + phone);
        redisUtil.del("FULOGIN" + newPhone);

        Integer userId = JwtUtil.getUserId(req.getHeader("X-Access-Token"));
        userhome = userhomeService.getById(userId);
        userhome.setPhone(newPhone);
        userhome.setUpdateTime(new Date());
        userhomeService.updateById(userhome);
        result.success("更换手机号成功");
        redisUtil.del("AT" + userhome.getId() + userhome.getPhone());
        userhomeService.setAccessToken(userhome.getId(), "token");
        return result;
    }


    /**
     * 退出登录
     *
     * @return
     */
    @ApiOperation("退出登录")
    @PostMapping(value = "/logout")
    public Result<Object> logout(HttpServletRequest req) {
        //用户退出逻辑
        String token = req.getHeader(CommonConstant.X_ACCESS_TOKEN);
        if (oConvertUtils.isEmpty(token)) {
            return Result.error("退出登录失败！");
        }
        Integer userId = JwtUtil.getUserId(token);
        Userhome userhome = userhomeService.getById(userId);
        if (userhome != null) {
            redisUtil.del("AT" + userhome.getId() + userhome.getPhone());
            userhomeService.setAccessToken(userhome.getId(), "token");
            return Result.ok("退出登录成功！");
        } else {
            return Result.error("Token无效!");
        }
    }


    /**
     * 头像上传
     *
     * @return
     */
    @ApiOperation("头像上传")
    @PostMapping(value = "/avatar")
    public Result<Object> avatar(@RequestBody Userhome upUserhome, HttpServletRequest req) {
        Integer userId = JwtUtil.getUserId(req.getHeader(CommonConstant.X_ACCESS_TOKEN));
        Userhome userhome = userhomeService.getById(userId);
        if (userhome != null) {
            userhome.setAvatar(upUserhome.getAvatar());
            userhomeService.updateById(userhome);
        }
        return Result.ok("头像修改成功");
    }

    /**
     * 发送短信
     *
     * @return
     */
    @ApiOperation("发送短信")
    @PostMapping(value = "/sms")
    public Result<String> sms(@RequestBody PhoneCode phoneCode) {
        Result<String> result = new Result<String>();
        String mobile = phoneCode.getPhone();
        //手机号模式 登录模式: "2"  注册模式: "1"
        //String smsmode = phoneCode.getType();
        log.info(mobile);
        if (oConvertUtils.isEmpty(mobile)) {
            result.setMessage("手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }
        String redisKey = "FULSMS" + mobile;
        Object object = redisUtil.get(redisKey);
        if (object != null) {
            result.setMessage("验证码10分钟内，仍然有效！");
            result.setSuccess(false);
            return result;
        }
        //随机数
        String captcha = RandomUtil.randomNumbers(6);
        JSONObject obj = new JSONObject();
        obj.put("code", captcha);
        try {
            boolean b = false;
            b = DySmsHelper.sendSms(mobile, obj, DySmsEnum.LOGIN_FRONT_TEMPLATE_CODE);
            if (b == false) {
                result.setMessage("短信验证码发送失败,请稍后重试");
                result.setSuccess(false);
                return result;
            }
            //验证码10分钟内有效
            redisUtil.set(redisKey, captcha, 600);
            result.setSuccess(true);
        } catch (ClientException e) {
            e.printStackTrace();
            result.error500(" 短信接口未配置，请联系管理员！");
            return result;
        }
      /*  String captcha ="1234";
        JSONObject obj = new JSONObject();
        obj.put("code", captcha);
        redisUtil.set(redisKey, captcha, 600);*/
        result.setSuccess(true);
        return result;
    }



    /**
     * 发送短信
     *
     * @return
     */
    @ApiOperation("发送邮件")
    @PostMapping(value = "/mail")
    public Result<String> mail(@RequestBody MailCode mailCode) {
        Result<String> result = new Result<String>();
        String mail = mailCode.getMail();
        //手机号模式 登录模式: "2"  注册模式: "1"
        //String smsmode = phoneCode.getType();
        log.info(mail);
        if (oConvertUtils.isEmpty(mail)) {
            result.setMessage("邮箱不允许为空！");
            result.setSuccess(false);
            return result;
        }
        String redisKey = "FULOGIN" + mail;
        Object object = redisUtil.get(redisKey);
        if (object != null) {
            result.setMessage("验证码30分钟内，仍然有效！");
            result.setSuccess(false);
            return result;
        }
        //随机数
       String captcha = RandomUtil.randomNumbers(6);
        JSONObject obj = new JSONObject();
        obj.put("code", captcha);
        try {
            String context="<html><body>您的验证码:"
                    +"<p>Enduring Vision Capital Co., Ltd.</p>"
                    +"<p>尊敬的客户：</p>"
                    +"<p>感谢阁下提出的申请。</p>"
                    + "<p>以下是阁下登入帐户的一次性验证码：</p>"
                    + "<p>激活代码:"+captcha+"</p>"
                    +"<p>请注意，此验证码的有效时间为30分钟。过期后，你需要再次提交申请，以获取一个新的验证码。</p>"
                    +"<p>此致，</p>"
                    +"<p>Enduring Vision Capital Co., Ltd.</p>"
                    +"</html></body>";
            sysBaseApi.sendEmailMsg(mail,"長觀驗證碼",context);
            //验证码10分钟内有效
            redisUtil.set(redisKey, captcha, 1800);
            result.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("邮箱验证未配置，请联系管理员！");
            return result;
        }
      /*  String captcha ="1234";
        JSONObject obj = new JSONObject();
        obj.put("code", captcha);
        redisUtil.set(redisKey, captcha, 600);*/
        result.setSuccess(true);
        return result;
    }


    @ApiOperation("注册接口")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result<JSONObject> register(@RequestBody RegisterUserhome registerUserhome) {
        Result<JSONObject> result = new Result<JSONObject>();
        String mail = registerUserhome.getMail();
        String phone = registerUserhome.getPhone();
        String password = registerUserhome.getPassword();
        String code = registerUserhome.getCode();
        String code2 = registerUserhome.getSmsCode();
        if (oConvertUtils.isEmpty(phone)) {
            result.setMessage("手机号不允许为空！");
            result.setSuccess(false);
            return result;
        }
        if (oConvertUtils.isEmpty(mail)) {
            result.setMessage("邮箱不允许为空！");
            result.setSuccess(false);
            return result;
        }
        if (code == null) {
            result.error500("邮箱验证码无效");
            return result;
        }
        Object checkCode = redisUtil.get("FULOGIN" + mail);
        if (checkCode == null || !checkCode.toString().equals(code)) {
            result.error500("邮箱验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        if (code2 == null) {
            result.error500("短信验证码无效");
            return result;
        }
        Object checkCode2 = redisUtil.get("FULSMS" + phone);
        if (checkCode2 == null || !checkCode2.toString().equals(code2)) {
            result.error500("短信验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        LambdaQueryWrapper<Userhome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userhome::getUsername, mail);
        Userhome userhome = userhomeService.getOne(queryWrapper);
        if (userhome == null) {
            userhome = new Userhome();
            userhome.setUsername(mail);
            userhome.setPhone(phone);
            userhome.setStatus(1);
            userhome.setCreateTime(new Date());
            userhome.setUpdateTime(new Date());
            userhome.setPassword(password);
            Random random = new Random();
            userhome.setSalt(String.valueOf(random.nextInt(90000000) + 10000000));
            //产生邀请码
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String inviteCode = now.format(formatter);
            userhome.setInviteCode(inviteCode);
            if (oConvertUtils.isNotEmpty(registerUserhome.getInviterCode())) {
                LambdaQueryWrapper<Userhome> queryWrapperInviteCode = new LambdaQueryWrapper<>();
                queryWrapperInviteCode.eq(Userhome::getInviteCode, registerUserhome.getInviterCode());
                Userhome userhomeInvite = userhomeService.getOne(queryWrapperInviteCode);
                if(oConvertUtils.isEmpty(userhomeInvite))
                {
                    result.error500("邀请码不存在");
                    return result;
                }
                //邀请人+1
                userhomeInvite.setInviteNum(userhomeInvite.getInviteNum()+1);
                userhomeService.updateById(userhomeInvite);
                userhome.setInviterId(userhomeInvite.getId());
                userhome.setInviterPhone(userhomeInvite.getPhone());
                userhome.setInviterUsername(userhomeInvite.getUsername());
            }
            userhomeService.save(userhome);
            String userpassword = PasswordUtil.encrypt(String.valueOf(userhome.getId()), password, userhome.getSalt());
            userhome.setPassword(userpassword);
            userhomeService.updateById(userhome);
            result.setSuccess(true);

        } else {
            result.error500("该用户已存在");
            return result;
        }
        //用户登录信息
        userhome.setPassword(null);
        userhome.setSalt(null);
        redisUtil.del("FULOGIN" + mail);
        redisUtil.del("FULSMS" + mail);
        return result;
    }


    /**
     * 头像上传
     *
     * @return
     */
    @ApiOperation("绑定关系")
    @PostMapping(value = "/bind")
    public Result<JSONObject> bind(@RequestBody BindInviteCode bindInviteCode, HttpServletRequest req) {
        Result<JSONObject> result = new Result<JSONObject>();
        Integer userId = JwtUtil.getUserId(req.getHeader(CommonConstant.X_ACCESS_TOKEN));
        Userhome userhome = userhomeService.getById(userId);
        if (oConvertUtils.isNotEmpty(userhome.getInviterId())) {
            result.error500("已经有绑定关系");
            return result;
        }
        String code=bindInviteCode.getCode();
        if (code == null) {
            result.error500("验证码无效");
            return result;
        }
        Object checkCode = redisUtil.get("FULOGIN" + userhome.getUsername());
        if (checkCode == null || !checkCode.toString().equals(code)) {
            result.error500("验证码错误");
            result.setCode(HttpStatus.PRECONDITION_FAILED.value());
            return result;
        }
        if (oConvertUtils.isNotEmpty(bindInviteCode.getInviterCode())) {
            LambdaQueryWrapper<Userhome> queryWrapperInviteCode = new LambdaQueryWrapper<>();
            queryWrapperInviteCode.eq(Userhome::getInviteCode, bindInviteCode.getInviterCode());
            Userhome userhomeInvite = userhomeService.getOne(queryWrapperInviteCode);
            if(oConvertUtils.isEmpty(userhomeInvite))
            {
                result.error500("邀请码不存在");
                return result;
            }
            if(userhomeInvite.getId().equals(userhome.getId()))
            {
                result.error500("不能绑定自己");
                return result;
            }
            userhome.setInviterId(userhomeInvite.getId());
            userhome.setInviterPhone(userhomeInvite.getPhone());
            userhome.setInviterUsername(userhomeInvite.getUsername());
            userhomeService.updateById(userhome);
            //邀请人+1
            userhomeInvite.setInviteNum(userhomeInvite.getInviteNum()+1);
            userhomeService.updateById(userhomeInvite);
            result.setSuccess(true);
        }
        else
        {
            result.error500("请输入邀请码");
            return result;
        }
        redisUtil.del("FULOGIN" + userhome.getPhone());
        return Result.ok("绑定关系成功");
    }

}